import { createRouter, createWebHistory } from 'vue-router'


const routes = [
  {
    path: '/',
   redirect:'/index'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@views/Home/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@views/Home/Register.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@views/Home/Home.vue')
  },
  {
    path: '/:catchAll(.*)',
    name: '/404',
    component: () => import('@components/404.vue')
  },
  {
    path: '/index',
    name: 'index',
    component:()=>import('@views/Main/Index.vue'),
    children: [
      { path: '', component: ()=>import('@views/Home/Home.vue') },
     
    ]
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to,from,next)=>{
  if (to.path==='/login'||to.path==='register') {
    next()
  }
  else{
    localStorage.getItem('token')? next(): next({path:'/login'})
  }
})

export default router
